package com.xiaoyu.base.model.auth;

import android.text.TextUtils;

import com.xiaoyu.base.app.AppContext;
import com.xiaoyu.base.data.UserData;

import com.xiaoyu.base.data.UserKVConfigData;
import com.xiaoyu.base.utils.KeyUtil;

import in.srain.cube.request.JsonData;
import in.srain.cube.util.StringUtil;
import in.srain.cube.util.time.TimeData;

public class TokenInfo {

    private String mAccessToken;
    private String mRefreshToken;
    private String mSecretKey;
    private long mAccessTokenLiveTime;
    private long mRefreshTokenLiveTime;

    private JsonData mCookie;
    private String mCookieName;
    private String mCookiePath;
    private JsonData mCookieDomains;

    public void restoreFromJson(JsonData jsonData) {
        mAccessToken = jsonData.optString("accessToken");
        mRefreshToken = jsonData.optString("refreshToken");
        mSecretKey = jsonData.optString("secretKey", KeyUtil.key(AppContext.getContext().getAssets()));
        mAccessTokenLiveTime = jsonData.optLong("accessTokenLiveTime");
        mRefreshTokenLiveTime = jsonData.optLong("refreshTokenLiveTime");

        mCookie = jsonData.optJson("cookie");
        mCookieName = mCookie.optString("name");
        mCookiePath = mCookie.optString("path");
        mCookieDomains = mCookie.optJson("domains");
    }

    private void save() {
        UserData.getInstance().saveUserKV(UserKVConfigData.KEY_TOKEN_INFO, toJson());
    }

    public synchronized void updateFromJson(JsonData jsonData) {
        mAccessToken = jsonData.optString("accessToken");
        mRefreshToken = jsonData.optString("refreshToken");
        mSecretKey = jsonData.optString("secretKey", KeyUtil.key(AppContext.getContext().getAssets()));
        mAccessTokenLiveTime = jsonData.optLong("accessTokenLiveTime");
        mRefreshTokenLiveTime = jsonData.optLong("refreshTokenLiveTime");

        mCookie = jsonData.optJson("cookie");
        mCookieName = mCookie.optString("name");
        mCookiePath = mCookie.optString("path");
        mCookieDomains = mCookie.optJson("domains");

        // TODO Need remove
        if (!mCookieDomains.has("lanling") && !TextUtils.isEmpty(mCookie.optString("domain"))) {
            mCookieDomains.put("lanling", mCookie.optString("domain"));
        }

        save();
    }

    public JsonData toJson() {
        final JsonData jsonData = JsonData.newMap();
        jsonData.put("accessToken", mAccessToken);
        jsonData.put("refreshToken", mRefreshToken);
        jsonData.put("secretKey", mSecretKey);
        jsonData.put("accessTokenLiveTime", mAccessTokenLiveTime);
        jsonData.put("refreshTokenLiveTime", mRefreshTokenLiveTime);
        jsonData.put("cookie", mCookie);
        return jsonData;
    }

    public boolean isValidate() {
        return !TextUtils.isEmpty(mAccessToken)
                && !TextUtils.isEmpty(mRefreshToken)
                && !TextUtils.isEmpty(mSecretKey)
                && mAccessTokenLiveTime > 0
                && mRefreshTokenLiveTime > 0;
    }

    public boolean shouldRenew() {
        if (!isValidate()) {
            return false;
        }
        final long currentServerTime = TimeData.getInstance().getServerTime().getTime();
        return currentServerTime >= mAccessTokenLiveTime;
    }

    @Override
    public String toString() {
        return toJson().toString();
    }

    public String getAccessToken() {
        return mAccessToken;
    }

    public String getRefreshToken() {
        return mRefreshToken;
    }

    public String getSecretKey() {
        return mSecretKey;
    }

    public JsonData getCookie() {
        return mCookie;
    }

    public String getCookieName() {
        return mCookieName;
    }

    public String getCookiePath() {
        return mCookiePath;
    }

    public JsonData getCookieDomains() {
        return mCookieDomains;
    }

    public void clear() {
        mAccessToken = null;
        mRefreshToken = null;
        mAccessTokenLiveTime = 0;
        mRefreshTokenLiveTime = 0;
        mSecretKey = null;
        mCookie = null;
        mCookieName = null;
        mCookiePath = null;
        mCookieDomains = null;

        save();
    }
}
